package com.gadgets.busi.bm01.controller;

import com.gadgets.busi.bm01.common.GadgetsBm01Constant;
import com.gadgets.busi.bm01.common.ImgCopyRestValidateTrigger;
import com.gadgets.busi.bm01.dto.ImgCopyRestDTO;
import com.gadgets.busi.bm01.service.IImgCopyService;
import com.gadgets.busi.bm01.task.Copy1024ImgRunnable;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-02-15
 */
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/1024")
public class Img1024CopyController implements IImgCoptController{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImgCopyRestValidateTrigger imgCopyRestValidateTrigger;
    @Autowired
    @Qualifier("img1024CopyService")
    private IImgCopyService imgCopyService;

    /**
     * 文件上传基础路径
     */
    @Value("${spring.uploadPath}")
    private String uploadPath;
    @Value("${spring.downloadPath}")
    private String downloadPath;
    @Value("${txt.1024.targetPath}")
    private String path;

    @SuppressWarnings("rawtypes")
    //@RoleAuthCheck
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/imgCopy.do")
    @Override
    public HttpBaseResp copyImg(HttpServletRequest request, HttpServletResponse response, ImgCopyRestDTO imgCopyRestDTO) throws GadgetsServiceException {
        imgCopyRestValidateTrigger.validate(imgCopyRestDTO);
        String[] targetPaths = imgCopyRestDTO.getPath().split(",");
        if(GadgetsBm01Constant.COPY_MODUEL.MULTI_COPY.equals(imgCopyRestDTO.getCopyModuel())){
            // 组合拷贝
            logger.info("执行组合拷贝");
            Copy1024ImgRunnable runnable =  new Copy1024ImgRunnable();
            runnable.setModuelNo(GadgetsBm01Constant.BM01_MODUEL_NO);
            runnable.setTargetSite(imgCopyRestDTO.getTargetSite());
            runnable.setPath(targetPaths);
            runnable.setTitle(imgCopyRestDTO.getDirName());
            runnable.setWritePath(path);
            runnable.setCopyModuel(GadgetsBm01Constant.COPY_MODUEL.MULTI_COPY);
            imgCopyService.submitImgCopyTask(runnable);
        }else {
            logger.info("执行独立拷贝");
            for (String targetPath : targetPaths) {
                // 独立拷贝
                Copy1024ImgRunnable runnable =  new Copy1024ImgRunnable();
                runnable.setModuelNo(GadgetsBm01Constant.BM01_MODUEL_NO);
                runnable.setTargetSite(imgCopyRestDTO.getTargetSite());
                runnable.setTitle(imgCopyRestDTO.getDirName());
                runnable.setPath(new String[]{targetPath});
                runnable.setWritePath(path);
                runnable.setCopyModuel(GadgetsBm01Constant.COPY_MODUEL.SINGLE_COPY);
                imgCopyService.submitImgCopyTask(runnable);
            }
        }
        return new HttpBaseResp();
    }

}
